<template>
  <div class="custom-login-container">
    <div class="custom-login-box">
      <div class="login-form">
        <div class="form-title">欢迎登录</div>
        <el-form :model="data.form" :rules="rules" ref="formRef" class="form-container">
          <el-form-item prop="username">
            <el-input v-model="data.form.username" prefix-icon="el-icon-user" placeholder="请输入账号" clearable></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="data.form.password" show-password prefix-icon="el-icon-lock" placeholder="请输入密码" clearable></el-input>
          </el-form-item>
          <el-form-item prop="role">
            <el-radio-group v-model="data.form.role">
              <el-radio label="ADMIN">教师</el-radio>
              <el-radio label="STUDENT">学生</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item class="login-button">
            <el-button type="primary" @click="login" style="width: 100%">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import router from "@/router";

const data = reactive({
  form: { role: 'ADMIN' }
});

const rules = reactive({
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
});

const formRef = ref();

const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/login', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success("登录成功");
          localStorage.setItem('student-user', JSON.stringify(res.data));
          router.push('/home'); // 跳转至主页
          console.log(res.data);
        } else {
          ElMessage.error(res.msg);
        }
      });
    }
  });
};
</script>

<style scoped>
.custom-login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f0f0;
  background-image: url("https://th.bing.com/th/id/R.50b10251bc3736c42599a1b55daa0789?rik=xTWctKXE6Y7LWQ&riu=http%3a%2f%2fwww.yueshi-edu.com%2fuploads%2fallimg%2f2006%2f201911181574089307137529.jpg&ehk=%2boAV30Rxu%2fFcuMzj5jwooZ2WmrHLDI3cBTYftdOEYiQ%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1");
  background-size: cover;
}

.custom-login-box {
  width: 100%;
  max-width: 360px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
  border-radius: 8px;
}

.login-form {
  margin-bottom: 20px;
}

.form-title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}

.form-container {
  padding: 20px;
}

.login-button {
  margin-top: 20px;
  text-align: center;
}

.register-link {
  text-align: center;
  margin-top: 10px;
}

.register-link a {
  color: #409eff;
  cursor: pointer;
}
</style>
